import get_f from '@dfeidao/atom-nodejs/data/get-field';
import complex_query from '@dfeidao/atom-nodejs/db/complex-query';
import log from '@dfeidao/atom-nodejs/logger/log';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../atom/config';
interface Message {
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	params: { productid: string, class_id: string, class_res: [{ class_id: string, class_name: string, grade: string }] };
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}
interface D {
	class_id: string;
	class_name: string;
	qd: number;
	qj: number;
	count: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:app-home/s002,action_id:' + action_id);
	const query = complex_query(action_id, session_id, systemid);
	// 依据“班级编号”从<班级学生>表读取“学生编号”，“学生姓名”以“学生编号”条数求和，得到总人数
	const class_student_res = await query
		.add_field('class_student', 'student_no', 'student_no')
		.add_field('class_student', 'student_name', 'student_name')
		.add_field('class_student', 'status', 'status')
		.where_eq('class_student', 'class_id', message.params.class_id)
		.where_eq('class_student', 'productid', message.params.productid)
		.exec() as [{ status: string }];

	const class_student = new Array(); // 未请假/已签到
	class_student_res.forEach((v) => {
		if (v.status !== '1') {
			class_student.push(v);
		}
	});
	// 1.3.1、依据“班级编号”从<班级学生>表读取“学生编号”“学生姓名”，以“学生编号”和当前系统日期为条件查询<签到>表，如果查询到数据，该学生不加入页面“未签到”人数计算，如果没有查到数据，将“学生编号”条数求和，得到总人数，并显示到页面未签到人数
	const data = new Date();
	const student_no = get_f(class_student, 'student_no');
	const yqd = await query
		.add_field('check_in', 'student_no', 'student_no')
		.add_field('check_in', 'check_in_status', 'check_in_status')
		.where_in('check_in', 'student_no', student_no)
		.where_eq('check_in', 'productid', message.params.productid)
		.where_gte('check_in', 'arrive_time', data.setHours(0, 0, 0, 0))
		.where_lte('check_in', 'arrive_time', data.setHours(23, 59, 59, 999))
		.exec();

	const wqd = class_student.length - yqd.length;
	// 1.3.3、  依据“班级编号”从<班级学生>表中读取“学生编号”“状态”=“1”的学生，计算人数展示在页面“请假”人数
	const qj = class_student_res.length - class_student.length;

	// 1.4.1、把查询到的剩余班级的“年级名称”“班级名称”组合展示在页面下方一个个小方框顶部
	// 1.4.2、“签到”： 依据“教师编号”，“班级编号”，当前系统“日期”从<签到>表中读取“学生编号”，计算人数展示在“/”前边
	// 依据“班级编号”从<班级学生>表读取“学生编号”，“学生姓名”以“学生编号”条数求和，得到总人数展示在“/”后边
	// 依据“班级编号”从<班级学生>表中读取“学生编号”“状态”=“1”的学生，计算人数展示在页面“请假”人数
	const ids = new Array();
	message.params.class_res.filter((v) => {
		if (v.class_id !== message.params.class_id) {
			ids.push(v.class_id);
		}
	});

	const zr_list = await query // 总人数
		.add_field('class_student', 'class_id', 'class_id')
		.add_fun('class_student', 'class_id', 'count', 'count')
		.where_in('class_student', 'class_id', ids)
		.where_eq('class_student', 'productid', message.params.productid)
		.groupby('class_student', 'class_id')
		.exec() as D[];
	const qj_list = await query // 请假
		.add_field('class_student', 'class_id', 'class_id')
		.add_fun('class_student', 'class_id', 'count', 'count')
		.where_in('class_student', 'class_id', ids)
		.where_eq('class_student', 'status', '1')
		.where_eq('class_student', 'productid', message.params.productid)
		.groupby('class_student', 'class_id')
		.exec() as D[];
	const datas = new Date();
	const yqd_list = await query // 签到
		.add_field('check_in', 'class_id', 'class_id')
		.add_fun('check_in', 'class_id', 'count', 'count')
		.outer_left_join('check_in', 'class_student', ['student_no', 'student_no'])
		.where_neq('class_student', 'status', '1')
		.where_in('check_in', 'class_id', ids)
		.where_eq('check_in', 'productid', message.params.productid)
		.where_gte('check_in', 'arrive_time', datas.setHours(0, 0, 0, 0))
		.where_lte('check_in', 'arrive_time', datas.setHours(23, 59, 59, 999))
		.groupby('check_in', 'class_id')
		.exec() as D[];
	zr_list.forEach((v) => {
		v.qd = 0;
		v.qj = 0;
		for (let i = 0; i < message.params.class_res.length; i++) {
			if (v.class_id === message.params.class_res[i].class_id) {
				v.class_name = message.params.class_res[i].grade + ' -' + message.params.class_res[i].class_name;
			}
		}
		for (let i = 0; i < qj_list.length; i++) {
			if (v.class_id === qj_list[i].class_id) {
				v.qj = qj_list[i].count;
			}
		}
		for (let i = 0; i < yqd_list.length; i++) {
			if (v.class_id === yqd_list[i].class_id) {
				v.qd = yqd_list[i].count;
			}
		}
	});
	// class_id:"BJ0002"
	// class_name:"六年级 -英语一班"
	// count:10
	// qd:0
	// qj:0
	const zr_list_1 = new Array();
	message.params.class_res.forEach((v) => {
		let cou = 0;
		for (let i = 0; i < zr_list.length; i++) {
			if (v.class_id === zr_list[i].class_id) {
				zr_list_1.push(zr_list[i]);
				break;
			} else {
				cou++;
			}
		}
		if (message.params.class_id !== v.class_id) {
			if (cou === zr_list.length) {
				zr_list_1.push({
					class_id: v.class_id,
					class_name: v.grade + ' -' + v.class_name,
					count: 0,
					qd: 0,
					qj: 0
				});
			}
		}
	});
	log('Service end path:app-home/s002,action_id:' + action_id);
	return {
		data: { class_student_res, qj, yqd: yqd.length, wqd, zr_list: zr_list_1, yqd_list }
	};
}
